#!/bin/bash

# 输出可执行文件名
_out="main"
# 定义编译器
_cc="gcc"

# a库链接
_a=""
# so库链接
_lso="-lpthread -lm"

#其他编译项
_other="--std=c99 -g -Wall"

# 定义.c源文件所在列表
declare -a _src_dirs
_src_dirs[0]="./src"
_src_dirs[1]="../utils"
# 工具包中忽略的源码文件
declare -a _utils_ignore_files
_utils_ignore_files[0]="map.c"

# 定义所有头文件所在路径
declare -a _include_dirs
_include_dirs[0]="./include"
_include_dirs[1]="../utils"


# 所有源文件字符串
_src_files_strings=""
#echo ${_src_dirs[0]}
for index in "${!_src_dirs[@]}"; do
    # echo "_src_dirs[$index] = ${_src_dirs[$index]}"
    for file in  $(ls ${_src_dirs[$index]}); do
        let ignore=0
        for ignore_index in "${!_utils_ignore_files[@]}";do
            if [[ "$file" == "${_utils_ignore_files[$ignore_index]}" ]]; then
                echo -e "ignore ${file}"
                let ignore=1
            fi
        done
        if (( ignore!=1 ));then
            if [[ $file == *.c ]] || [[ $file == *.cc ]] || [[ $file == *.cpp ]]; then
                _src_files_strings+="${_src_dirs[$index]}/$file " # 字符串拼接
            fi
        fi
    done
done
echo -e "all src file:\n${_src_files_strings}"

_include_dirs_strings=""
for index in "${!_include_dirs[@]}"; do
    _include_dirs_strings+=" -I ${_include_dirs[$index]}"
done
echo -e "include string:\n${_include_dirs_strings}"


gcc ${_src_files_strings} ${_include_dirs_strings} -o ${_out} ${_other} ${_lso} ${_a}
